package com.xxc.domain.strategy.repository;


import com.xxc.domain.strategy.model.entity.StrategyAwardEntity;
import com.xxc.domain.strategy.model.entity.StrategyEntity;
import com.xxc.domain.strategy.model.entity.StrategyRuleEntity;
import com.xxc.domain.strategy.model.valobj.RuleTreeVO;
import com.xxc.domain.strategy.model.valobj.StrategyAwardRuleModelVO;
import com.xxc.domain.strategy.model.valobj.StrategyAwardStockKeyVO;

import java.util.List;
import java.util.Map;

/**
 * @Author: xxc
 * @Description: 策略仓储接口
 * @Date: 2025/1/19 19:31
 * @Version: 1.0
 */
public interface IStrategyRepository {

    /**
     * 查找策略奖品表
     * @param strategyId
     * @return
     */
    List<StrategyAwardEntity> queryStrategyAwardList(Long strategyId);

    /**
     * 保存奖品查找表到Redis，同时把rateRange也存进Redis
     * @param key
     * @param rateRange
     * @param shuffledStrategyAwardRateSearchTable
     */
    void storeStrategyAwardSearchRateTable(String key, Integer rateRange, Map<Integer, Integer> shuffledStrategyAwardRateSearchTable);

    /**
     * 抽奖，根据策略id和随机值（rateRange之内），获取奖品id
     * @param key
     * @param rangeKey
     * @return
     */
    Integer getAssembledStrategyAwardId(String key, Integer rangeKey);

    /**
     * 从Redis取rateRange
     * @param strategyId
     * @return
     */
    int getRateRange(Long strategyId);

    int getRateRange(String key);

    StrategyEntity queryStrategyEntityByStrategyId(Long strategyId);

    /**
     * 策略装配的时候用的，查对策略总体生效的ruleModel的信息
     * @param strategyId 策略id
     * @param ruleModel
     * @return
     */
    StrategyRuleEntity queryStrategyRule(Long strategyId, String ruleModel);


    String queryStrategyRuleValue(Long strategyId, String ruleModel);

    String queryStrategyRuleValue(Long strategyId, Integer awardId, String ruleModel);

    StrategyAwardRuleModelVO queryStrategyAwardRuleModelVO(Long strategyId, Integer awardId);

    /**
     * 根据规则树ID，查询树结构信息
     *
     * @param treeId 规则树ID
     * @return 树结构信息
     */
    RuleTreeVO queryRuleTreeVOByTreeId(String treeId);

    /**
     * 缓存奖品库存
     *
     * @param cacheKey   key
     * @param awardCount 库存值
     */
    void cacheStrategyAwardCount(String cacheKey, Integer awardCount);

    /**
     * 缓存key，decr 方式扣减库存
     *
     * @param cacheKey 缓存Key
     * @return 扣减结果
     */
    Boolean subtractionAwardStock(String cacheKey);

    /**
     * 写入奖品库存消费队列
     *
     * @param strategyAwardStockKeyVO 对象值对象
     */
    void awardStockConsumeSendQueue(StrategyAwardStockKeyVO strategyAwardStockKeyVO);

    /**
     * 获取奖品库存消费队列
     */
    StrategyAwardStockKeyVO takeQueueValue() throws InterruptedException;

    /**
     * 更新奖品库存消耗
     *
     * @param strategyId 策略ID
     * @param awardId 奖品ID
     */
    void updateStrategyAwardStock(Long strategyId, Integer awardId);

    StrategyAwardEntity queryStrategyAwardEntity(Long strategyId, Integer awardId);
}

